package com.fmblzf.library.popwindow;

import android.content.Context;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.PopupWindow;

import com.example.administrator.library.R;
import com.fmblzf.library.edit.EditTextOnlyNumber;
import com.fmblzf.library.inter.Listener;

/**
 * Created by Administrator on 2015/11/12.
 */
public class PopWindowEditText {

    private EditTextOnlyNumber eton_1, eton_2,eton_3,eton_4,eton_5,eton_6;//密码框

    private EditTextOnlyNumber curEton;//当前输入框

    private ImageView ivImg;//返回图标

    private PopupWindow popupWindow;//弹出框

    private Listener listener = new Listener() {
        @Override
        public Object action(int flag, Object... objs) {
            Log.i("",String.valueOf(flag));
            return null;
        }
    };

    /**
     * 显示弹出框
     *
     */
    public void showPopWindow(Context context,View popView) {
        int width = context.getResources().getDisplayMetrics().widthPixels;
        int height = context.getResources().getDisplayMetrics().heightPixels;
        View view = LayoutInflater.from(context).inflate(R.layout.popwindow_layout_style, null);
        //返回图标
        ivImg = (ImageView) view.findViewById(R.id.iv_back_img);
        ivImg.setOnClickListener(new ImgOnClickListener());
        //获取控件
        eton_1 = (EditTextOnlyNumber) view.findViewById(R.id.eton_input_1);
        eton_2 = (EditTextOnlyNumber) view.findViewById(R.id.eton_input_2);
        eton_3 = (EditTextOnlyNumber) view.findViewById(R.id.eton_input_3);
        eton_4 = (EditTextOnlyNumber) view.findViewById(R.id.eton_input_4);
        eton_5 = (EditTextOnlyNumber) view.findViewById(R.id.eton_input_5);
        eton_6 = (EditTextOnlyNumber) view.findViewById(R.id.eton_input_6);
        //设置下一个控件
        eton_1.setNext(eton_2);
        eton_2.setNext(eton_3);
        eton_3.setNext(eton_4);
        eton_4.setNext(eton_5);
        eton_5.setNext(eton_6);
        //设置监听事件
        eton_6.setListener(listener);

//        ShowKeyboard(context,eton_1);

        popupWindow = new PopupWindow(view, width, height,true);
        //软键盘不会挡着popupwindow
        popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        popupWindow.showAtLocation(popView, Gravity.CENTER, 0, 0);

    }

    /**
     * 显示软键盘
     * @param context
     * @param view
     */
    public  void ShowKeyboard(Context context,View view){
        InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(view,InputMethodManager.SHOW_FORCED);
    }

    /**
     * 监听事件实现类
     */
    private class ImgOnClickListener implements android.view.View.OnClickListener{

        @Override
        public void onClick(View view) {
            popupWindow.dismiss();
        }
    }

    /**
     * 获取输入的密码
     * @return
     */
    public String getPassWord(){
        return eton_1.getText()+""+eton_2.getText()+""+eton_3.getText()+""+eton_4.getText()+""+eton_5.getText()+""+eton_6.getText()+"";
    }

}
